local function getupvaluetable(func, inname)
    local i = 1
    while true do
        local name, value = debug.getupvalue(func, i)
        if name == nil then
            return
        end
        
        if name == inname then
            return i,value
        end
        
        i=i+1
    end
end


local lua = _P.lua
-- local idx, old_func = getupvaluetable(lua.CMD,'frist_charge_rate')
local idx,common_post_web = getupvaluetable(lua.CMD.frist_charge_rate,'common_post_web')
local idx,reason = getupvaluetable(lua.CMD.frist_charge_rate,'reason')
local idx1,ADD_URL = getupvaluetable(lua.CMD.frist_charge_rate,'ADD_URL')
local idx2,EDIT_URL = getupvaluetable(lua.CMD.frist_charge_rate,'EDIT_URL')

print("___111111__",idx, ADD_URL)
print("___222222__",idx, EDIT_URL)

local function frist_charge_rate(uid,paid_amount,charge_count)

    
    dbglog("frist_charge_rate2222",uid,paid_amount,charge_count)

    local personal_control = global_configs.personal_control or {}
    local add_coins_rate  = (personal_control["charge_rate"] or 0) / 100
    local add_weight_rate = (personal_control["charge_weight"] or 0)
    if charge_count and charge_count == 1 then --首冲
        add_coins_rate  = (personal_control["frist_charge_rate"] or 0) / 100
        add_weight_rate = (personal_control["frist_charge_weight"] or 0)
        -- R().activitysvr(1):send('.activity_mgr','check_first_charge',uid,paid_amount)
    end
    if add_coins_rate == 0 then
        return
    end

    local add_coins = math.floor(paid_amount * add_coins_rate)
    local add_weight = add_weight_rate
    local ok,ret = R().basesvr({key = uid}):call('.msg_handler','add_control_coins_weight',uid,add_coins,add_weight,reason.CHARGE_RETRUN)
    dbglog("add_paid_product======3333",ok,tostring_r(ret))
    if ok and ret then
        if ret.add then
            local xcoins = add_coins / 100
            if xcoins > 0 then
                local info = {
                    control_amount  = xcoins,
                    weights = add_weight,
                    creation_type = charge_count == 1 and "首冲返水" or "充值返水",
                }
                common_post_web(uid,ADD_URL,info)
            end
        else
            local info = {
                control_amount  = ret.curr_control_coins / 100,
                weights = ret.curr_control_weight,
                creation_type = charge_count == 1 and "首冲返水" or "充值返水",
            }
            common_post_web(uid,EDIT_URL,info)
        end
    end
end

local function exhcange_coins(uid,coins)
    local personal_control = global_configs.personal_control or {}
    local reduce_coins_rate  = (personal_control["exchange_rate"] or 0) / 100
    local reduce_weight_rate = (personal_control["exchange_weight"] or 0) / 100
    local reduce_coins  = math.floor(coins * reduce_coins_rate)
    local reduce_weight = math.floor(coins/100 * reduce_weight_rate)
    if reduce_coins_rate == 0 then
        return
    end

    local ok,ret = R().basesvr({key = uid}):call('.msg_handler','reduce_control_coins_weight',uid,reduce_coins,reduce_weight,reason.CHARGE_RETRUN)
    dbglog("exhcange_coins======11111",ok,tostring_r(ret))
    if ok and ret then
        if ret.add then
            local xcoins = -(reduce_coins / 100)
            if xcoins < 0 then
                local info = {
                    control_amount  = -(reduce_coins / 100),
                    weights = reduce_weight,
                    creation_type = "吃分控制",
                }
                common_post_web(uid,ADD_URL,info)
            end
        else
            local info = {
                control_amount  = ret.curr_control_coins / 100,
                weights = ret.curr_control_weight,
                creation_type = "吃分控制",
            }
            common_post_web(uid,EDIT_URL,info)
        end
    end
end

print("___1__",type(lua.CMD))
print("___2__",type(lua.CMD.frist_charge_rate))
lua.CMD.frist_charge_rate = frist_charge_rate

lua.CMD.exhcange_coins = exhcange_coins